-- ~/.config/nvim/lua/config/keymaps.lua



local function setup_lsp_keymaps(bufnr)
  local opts = { noremap = true, silent = true, buffer = bufnr }
  local ft = vim.api.nvim_buf_get_option(bufnr, "filetype")

  -- 通用 LSP 键位
  vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
  vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
  vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
  vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, opts)

  -- 语言专用键位
  if ft == "python" then
    vim.keymap.set("n", "<leader>pt", "<cmd>PyrightTypeHierarchy<CR>", opts)
    vim.keymap.set("n", "<leader>pi", "<cmd>PyrightOrganizeImports<CR>", opts)
  elseif ft == "c" or ft == "cpp" then
    vim.keymap.set("n", "<leader>ch", "<cmd>ClangdSwitchSourceHeader<CR>", opts)
    vim.keymap.set("n", "<leader>cf", "<cmd>ClangdFormat<CR>", opts)
  end
end

-- 自动附加 LSP 键位
vim.api.nvim_create_autocmd("LspAttach", {
  callback = function(args)
    setup_lsp_keymaps(args.buf)
  end
})

-- 其他全局键位...



--========================================================================

vim.keymap.set("n", "<leader>n", "<Cmd>BufferLineCycleNext<CR>", { desc = "Next buffer" })
vim.keymap.set("n", "<S-Tab>", "<Cmd>BufferLineCyclePrev<CR>", { desc = "Prev buffer" })

-- ~/.config/nvim/lua/config/keymaps.lua
local filetree = require("core.filetree")

-- 主切换快捷键 (建议使用 <A-e> 或 <leader>tab)
vim.keymap.set("n", "<A-e>", filetree.toggle_focus, { desc = "Toggle file explorer focus" })

local buffer_mgmt = require("core.buffer_mgmt")
vim.keymap.set("n", "<leader>w", buffer_mgmt.smart_close, {
  desc = "关闭文件并聚焦剩余窗口（不跳转文件树）"
})

vim.keymap.set("n", "<leader>e", ":NvimTreeToggle<CR>")
